using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Threading;
using Services;
using SpaceBattle.Logic.SampleLevel;
using Utilities.Menu.UIElements;
using SpaceBattle.Resources;

namespace SpaceBattle.Menu
{

    using Game = Microsoft.Xna.Framework.Game;

    public class CampaignSucces : Microsoft.Xna.Framework.DrawableGameComponent
    {
        SpriteBatch sprite_batch;
        Matrix screen_transform;
        Texture2D bg, box_body, fwd;
        SpriteFont title_font, text_font, menu_font;
        Button back_to_menu;
        SoundEffect click;
        GameMode game_mode;

        BaseLevelStats user_data;

        bool initialized = false, increase_a = false;
        float alpha = 0.0f, wait_to_tap = 0.5f;

        public CampaignSucces(Game game, GameMode game_mode, BaseLevelStats user_data)
            : base(game)
        {
            game.Components.Add(new InputManager(game));

            this.user_data = user_data;
            this.game_mode = game_mode;
        }

        ContentManager Content { get { return Game.Content; } }
        public override void Initialize()
        {
            var input_service = Game.Services.GetService(typeof(IInputManager)) as IInputManager;
            input_service.OnTap += new InputManager.Tap(on_tap);

            user_data.TotalScore += user_data.LevelScore;

            base.Initialize();
        }

        protected override void LoadContent()
        {
            sprite_batch = Game.Services.GetService(typeof(SpriteBatch)) as SpriteBatch;
            screen_transform = Matrix.CreateScale(GraphicsDevice.Viewport.Width / SpaceBattleGame.DEFAULT_SCR_WIDTH, GraphicsDevice.Viewport.Height / SpaceBattleGame.DEFAULT_SCR_HEIGHT, 1.0f);
            bg = Content.Load<Texture2D>("LevelFinished/bg_png");
            fwd = Content.Load<Texture2D>("Menu/button_fwd");
            title_font = Content.Load<SpriteFont>("Fonts/OCRAExtended32");
            text_font = Content.Load<SpriteFont>("Fonts/OCRAExtended18");
            menu_font = Content.Load<SpriteFont>("Fonts/OCRAExtended24");
            box_body = Content.Load<Texture2D>("LevelFinished/victory_bg");
            click = Game.Content.Load<SoundEffect>("Audio/button");

            back_to_menu = new Button { CollisionBox = new Rectangle((int)(GraphicsDevice.Viewport.Width - 10 - fwd.Width), 
                (int)(GraphicsDevice.Viewport.Height / 2.0f - fwd.Height / 2.0f), fwd.Width, fwd.Height), 
                Label = "", Position = Vector2.Zero };

            box_pos = new Vector2(GraphicsDevice.Viewport.Width / 2.0f - box_body.Width / 2.0f, 
                GraphicsDevice.Viewport.Height / 2.0f - box_body.Height / 2.0f);

            base.LoadContent();
        }

        protected override void Dispose(bool disposing)
        {
            Game.Services.RemoveService(typeof(IInputManager));

            base.Dispose(disposing);
        }

        void on_tap(Vector2 tap_position)
        {
            if (wait_to_tap <= 0.0f)
            {
                var p = tap_position.ToPoint(screen_transform);

                if (back_to_menu.CollisionBox.Contains(p))
                {
                    Leaderboard.ResetCampaignState(false);
                    StateManager.SelectMenu(Game);
                    click.Play(AudioManager.Volume, 0.0f, 0.0f);
                }
            }
        }

        public override void Update(GameTime gameTime)
        {
            float dt = (float)gameTime.ElapsedGameTime.TotalSeconds;

            wait_to_tap -= dt;

            if (!initialized)
            {
                alpha = MathHelper.Clamp(alpha + dt * 2.0f, 0.0f, 1.0f);
                if (alpha == 1.0f)
                {
                    initialized = true;
                }
            }

            base.Update(gameTime);
        }

        Vector2 box_pos;
        public override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            sprite_batch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, screen_transform);

            sprite_batch.Draw(bg, new Rectangle(0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height), Color.White);

            sprite_batch.Draw(box_body, box_pos, Color.White * alpha);
            sprite_batch.DrawString(title_font, Strings.CampaginSucces, new Vector2(CenterValue(Game.GraphicsDevice.Viewport.Width, 
                title_font.MeasureString(Strings.CampaginSucces).X), 75), Color.White * alpha);
            sprite_batch.DrawString(text_font, Strings.TotalScore + user_data.TotalScore,
              new Vector2(Game.GraphicsDevice.Viewport.Width / 2.0f -
              text_font.MeasureString(Strings.TotalScore + user_data.TotalScore).X / 2.0f, 260), Color.White * alpha);

            sprite_batch.Draw(fwd, back_to_menu.CollisionBox, Color.White * alpha);

            sprite_batch.End();

            base.Draw(gameTime);
        }

        private float CenterValue(float screen_dim, float item_width)
        {
            return screen_dim / 2.0f - item_width / 2.0f;
        }
    }
}